package com.ocom.payconfig.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.payconfig.EntityReceiptAddress;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.adreceiptinfo.DeleteAdReceiptInfoRequest;
import com.ocom.common.request.payconfig.adreceiptinfo.GetAdReceiptInfoRequest;
import com.ocom.common.request.payconfig.adreceiptinfo.UpdateAdReceiptInfoRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoReceiptAddress;
import com.ocom.payconfig.service.ReceiptAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ReceiptAddressServiceImpl extends ServiceImpl<IDaoReceiptAddress, EntityReceiptAddress> implements ReceiptAddressService {

    @Autowired
    private IDaoReceiptAddress iDaoReceiptAddress;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityReceiptAddress> getAdReceiptInfo(Long companyId, GetAdReceiptInfoRequest request) {

        QueryWrapper<EntityReceiptAddress> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotNull(request.getComId())) {
            wrapper.eq("com_id", request.getComId());
        }

        if (StringUtils.isNotNull(request.getReceiptId())) {
            wrapper.eq("receipt_id", request.getReceiptId());
        }

        if (null != request.getParentReceiptId()) {
            wrapper.eq("parent_receipt_id", request.getParentReceiptId());
        }
        if (StringUtils.isNotEmpty(request.getReceiptName())) {
            wrapper.eq("receipt_name", request.getReceiptName());
        }
        //统计总量
//        Integer total = this.count(wrapper);
        if(ObjectUtil.isNotEmpty(request.getPointId()) && !request.getPointId().equals(-1)){
            wrapper.and(w->w.eq("point_id",0).or().eq("point_id",request.getPointId()));
        }
        if (null == request.getPage() || null == request.getSize() || (request.getPage() == -1 && request.getSize() == -1)) {
            request.setPage(1);
            request.setSize(500);
        }
        PageHelper.startPage(request.getPage(), request.getSize());

        return this.list(wrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean addAdReceiptInfo(Long companyId, EntityReceiptAddress request) {
        return this.save(request);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateAdReceiptInfo(Long companyId, UpdateAdReceiptInfoRequest request) {
        UpdateWrapper<EntityReceiptAddress> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", request.getComId());
        updateWrapper.eq("receipt_id", request.getReceiptId());

        updateWrapper.set("receipt_name", request.getReceiptName());
        updateWrapper.set("parent_receipt_id", request.getParentReceiptId());
        updateWrapper.set("receipt_orgin", request.getReceiptOrgin());
        updateWrapper.set("remark", request.getRemark());
        updateWrapper.set("update_user", request.getUpdateUser());
        return this.update(updateWrapper);

    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean deleteAdReceiptInfo(Long companyId, DeleteAdReceiptInfoRequest request) {
        QueryWrapper<EntityReceiptAddress> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", request.getComId());
        queryWrapper.eq("receipt_id", request.getReceiptId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer countByReceiptName(Long companyId, Long comId, String receiptName) {
        QueryWrapper<EntityReceiptAddress> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("receipt_name", receiptName);
        queryWrapper.last("limit 1");
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityReceiptAddress getByReceiptName(Long companyId, Long comId, String receiptName) {
        QueryWrapper<EntityReceiptAddress> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", comId);
        queryWrapper.eq("receipt_name", receiptName);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityReceiptAddress> getReceiptIds(Long companyId, String receiptName) {
        QueryWrapper<EntityReceiptAddress> queryWrapper = Wrappers.query();
        queryWrapper.select("receipt_id");
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("status", 1);
        queryWrapper.like("receipt_name", receiptName);
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityReceiptAddress> getReceiptInIds(Long companyId, List<Integer> receiptIds) {
        QueryWrapper<EntityReceiptAddress> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("status", 1);
        if (receiptIds != null && receiptIds.size() > 0) {
            queryWrapper.in("receipt_id", receiptIds);
        }
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateCodeUrl(Long companyId, Integer receiptId, String codeUrl, String codeUrl2) {
        UpdateWrapper<EntityReceiptAddress> updateWrapper = Wrappers.update();
        updateWrapper.eq("com_id", companyId);
        updateWrapper.eq("receipt_id", receiptId);

        if (StringUtils.isNotBlank(codeUrl)) {
            updateWrapper.set("code_pic_url", codeUrl);
        }
        if (StringUtils.isNotBlank(codeUrl2)) {
            updateWrapper.set("code_pic_url2", codeUrl2);
        }
        return this.update(updateWrapper);
    }
}
